﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Wicresoft.UnifyShow.Business;
using Microsoft.Practices.Unity;
using System.Windows.Forms.DataVisualization.Charting;
using System.IO;
using Wicresoft.UnifyShow.Portal.Extensions;

namespace Wicresoft.UnifyShow.Portal.Controllers
{
    public class PerformanceController : BaseController
    {

        private IPerformance performance;
        private IDevice device;
        private IUser_X user;


        protected override void Dispose(bool disposing)
        {
            ((IDisposable)performance).Dispose();
            ((IDisposable)device).Dispose();
            ((IDisposable)user).Dispose();
        }

        [InjectionConstructor]
        public PerformanceController(IPerformance performance, IDevice device, IUser_X user)
        {
            this.performance = performance;
            this.device = device;
            this.user = user;
        }

        public ActionResult GenerateReportDeviceID(int deviceID)
        {
            DeviceModel deviceM = device.GetDeviceByID(deviceID);
            base.SetRoles(user);
            if (!UserSiteIDs.Contains(deviceM.SiteId)) return new HttpStatusCodeResult(403);
            return View(deviceM);
        }

        public ActionResult ContentParitalView(int deviceID, DateTime startTime, DateTime endTime)
        {
            return PartialView(performance.GetPerformanceParametersByDeviceID(deviceID, startTime, endTime).
                Select(o => o.Parameter + "&" + o.ParameterDisplay).Distinct());
        }

        [OutputCache(CacheProfile = "Report", VaryByParam = "deviceID;parameter;startTime;endTime")]
        public ActionResult ReportParitalView(int deviceID, string parameterDisplay, DateTime startTime, DateTime endTime)
        {
            //声明Chart
            Chart chart = new Chart();
            chart.Size = new System.Drawing.Size(GlobalHelper.Width, GlobalHelper.Height);
            //设置图列样式颜色
            //chart.Legends[0].Font = GlobalHelper.fontLegend;
            //声明Series 和数据处理 
            Series series = new Series("Series");
            series.XValueType = ChartValueType.DateTime;
            series.ChartType = SeriesChartType.Line;
            //series.IsValueShownAsLabel = true;
            List<PerformanceModel> pm = new List<PerformanceModel>();
            pm = performance.GetPerformanceByDeviceIDAndParameterDisplay(deviceID, parameterDisplay, startTime, endTime);
            var items = pm.OrderBy(o => o.TimeStamp);
            foreach (var item in items)
            {
                series.Points.AddXY(item.TimeStamp, Double.Parse(item.Value));
            }
            //声明ChartArea
            ChartArea area = new ChartArea("Default");
            area.AxisX.IsMarginVisible = true;
            area.AxisX.MajorGrid.Enabled = false;
            area.AxisY.MajorGrid.Enabled = false;
            double max = 0;
            double min = 0;
            if (series.Points.Count != 0)
            {
                max = series.Points.FindMaxByValue().YValues[0] * 1.1;
                min = series.Points.FindMinByValue().YValues[0] * 0.9;

                area.AxisY.Title = "单位/" + pm.FirstOrDefault().Unit;
            }
            double differ;
            double intervalY;

            if (max > 1)
            {
                max = Math.Floor(max);
                min = Math.Floor(min);
                differ = (max - min) / 10.0;
                intervalY = (int)Math.Floor(differ) + 1;
            }
            else
            {
                max = Math.Round(max, 3) + 0.001;
                min = Math.Round(min, 3) - 0.001;
                differ = (max - min) / 10.0;
                intervalY = Math.Round(differ, 4) + 0.0001;
            }


            area.AxisY.Maximum = max;
            area.AxisY.Minimum = min;
            if (intervalY != 0) area.AxisY.Interval = intervalY;

            area.AxisX.Title = "日期";

            //添加 
            series.ChartArea = area.Name;
            chart.ChartAreas.Add(area);
            chart.Series.Add(series);
            //返回
            return new ChartResult(chart);
        }

    }
}
